package cn.jtfadmin.base.doc.swagger;

import cn.jtfadmin.base.lang.config.module.ModuleInfo;
import cn.jtfadmin.base.lang.constant.SecurityConstant;
import com.thoughtworks.qdox.JavaProjectBuilder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * swagger 自动配置
 * @author jtf
 * @since 0.0.1
 */
@Configuration
@ComponentScan("cn.jtfadmin.base.doc.swagger")
public class SwaggerConfig implements BeanDefinitionRegistryPostProcessor {


    public static List<RequestParameter> publicParam;


    /**
     * 公共请求头
     */
    static {
        publicParam = new ArrayList<>();
        RequestParameterBuilder token = new RequestParameterBuilder();
        RequestParameter t = token.name(SecurityConstant.AUTH_HEADER_NAME)
                .in(ParameterType.HEADER)
                .description("认证请求头")
                .required(false)
                .query(param->param.model(model->model.scalarModel(ScalarType.STRING)))
                .build();
        publicParam.add(t);
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        if(registry instanceof ConfigurableListableBeanFactory){
            ConfigurableListableBeanFactory beanFactory = ConfigurableListableBeanFactory.class.cast(registry);
            String[] beanNamesForType = beanFactory.getBeanNamesForType(ModuleInfo.class);
            for (String beanName : beanNamesForType) {
                ModuleInfo bean = beanFactory.getBean(beanName, ModuleInfo.class);
                String no = bean.getNo();
                RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(DockerProxy.class,
                        ()-> new DockerProxy(bean)
                );
                registry.registerBeanDefinition("doc_"+no, rootBeanDefinition);
            }
        }

    }
    @Bean
    @ConditionalOnMissingBean(JavaProjectBuilder.class)
    public JavaProjectBuilder javaProjectBuilder(){
        JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder();
        javaProjectBuilder.setEncoding("utf-8");
        javaProjectBuilder.addSourceTree(new File("src/main/java"));
        PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
        String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "/**/*.java";
        try {
            Resource[] resources = patternResolver.getResources(packageSearchPath);
            for (Resource resource : resources) {

                javaProjectBuilder.addSource(resource.getURL());
            }
        } catch (
                IOException e) {
            throw new RuntimeException(e);
        }
        return javaProjectBuilder;
    }


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
